{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 市场关注\n",
    "\n",
    "- 作者：DGC'Idea\n",
    "- 版本：V0.1  \n",
    "- 更新日期：2018年4月12日"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   amount amount_incr        amount_incr_tips amount_increase_rt apply_fee                                     apply_fee_tips apply_redeem_status apply_status asset_ratio discount_rt  est_val_dt est_val_increase_rt estimate_value fund_id fund_id_color fund_nav fund_nm fund_nm_color increase_rt index_id       index_nm    last_est_datetime last_est_dt last_est_time last_time lof_type min_amt money_cd      nav_dt notes  owned  price    price_dt redeem_fee                                  redeem_fee_tips redeem_status ref_increase_rt stock_volume turnover_rt   volume\n",
      "9  227363         788  最新份额：227363万份；增长：0.35%               0.35      1.5%  50万以下 1.5%；\\n50万(含)至100万 1.2%；\\n100万(含)至200万 1...           开放申购/开放赎回         开放申购      95.000      -1.23%  2019-03-18               2.25%         0.5123  162411        162411   0.5010    华宝油气          华宝油气       1.20%        Y  标普石油天然气上游股票指数  2019-03-19 15:00:03  2019-03-19      15:00:03  15:00:03     QDII    None      USD  2019-03-15            0  0.506  2019-03-19       1.5%  7日内1.5%\\n持有1年以下 0.50%；\\n持有1年至2年 0.25%；\\n持有2年以上0          开放赎回           2.25%   12507.7736       5.50%  6333.14\n",
      "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\" rules=\"rows\" frame=\"void\">\n",
      "  <tbody style=\"text-align:right;font-size:8px\">\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">估值</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>全市场</td>\n",
      "      <td>PE:30.88</td>\n",
      "      <td>23.94%偏低</td>\n",
      "      <td>PB:2.29</td>\n",
      "      <td>20.49%偏低</td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>关注</td>\n",
      "      <td>中证500</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">股指</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>上证综指</td>\n",
      "      <td>沪深300</td>\n",
      "      <td>创业板指</td>\n",
      "      <td>深证成指</td>\n",
      "      <td>恒生指数</td>\n",
      "      <td>BDI指数</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>3090.975</td>\n",
      "      <td>3833.963</td>\n",
      "      <td>1708.874</td>\n",
      "      <td>9839.744</td>\n",
      "      <td>29466.28</td>\n",
      "      <td>721</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>标普500</td>\n",
      "      <td>道琼斯</td>\n",
      "      <td>纳斯达克</td>\n",
      "      <td>德国DAX</td>\n",
      "      <td>英国富时</td>\n",
      "      <td>VIX波动</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>2832.94</td>\n",
      "      <td>25914.1</td>\n",
      "      <td>7714.48</td>\n",
      "      <td>11754.29</td>\n",
      "      <td>7322.5</td>\n",
      "      <td>12.69</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">商品</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>黄金</td>\n",
      "      <td>白银</td>\n",
      "      <td>金银比</td>\n",
      "      <td>布原油</td>\n",
      "      <td>WTI原油</td>\n",
      "      <td>金油比</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>1308.14</td>\n",
      "      <td>15.405</td>\n",
      "      <td>84.917</td>\n",
      "      <td>68.12</td>\n",
      "      <td>59.79</td>\n",
      "      <td>19.203</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">外汇</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>美元指数</td>\n",
      "      <td>离岸￥</td>\n",
      "      <td>在岸￥</td>\n",
      "      <td>港币/￥</td>\n",
      "      <td>＄/日元</td>\n",
      "      <td>比特币/＄</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>96.3455</td>\n",
      "      <td>6.7176</td>\n",
      "      <td>6.713</td>\n",
      "      <td>0.8551</td>\n",
      "      <td>111.2</td>\n",
      "      <td>3966.5</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">国债</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>美国10年</td>\n",
      "      <td>美国5年</td>\n",
      "      <td>美国3年</td>\n",
      "      <td>中国10年</td>\n",
      "      <td>中国5年</td>\n",
      "      <td>中国3年</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>96.3455</td>\n",
      "      <td>111.2</td>\n",
      "      <td>6.7176</td>\n",
      "      <td>3.158</td>\n",
      "      <td>3.041</td>\n",
      "      <td>2.798</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">套利</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>华宝油气</td>\n",
      "      <td>溢价率</td>\n",
      "      <td>当前价格</td>\n",
      "      <td>15日净值</td>\n",
      "      <td>18日估值</td>\n",
      "      <td>指数涨幅</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td></td>\n",
      "      <td>-1.23%</td>\n",
      "      <td>0.506</td>\n",
      "      <td>0.501</td>\n",
      "      <td>0.512</td>\n",
      "      <td>2.25%</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">逆回购</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>沪市</td>\n",
      "      <td>2.915/1</td>\n",
      "      <td>2.955/2</td>\n",
      "      <td>2.980/3</td>\n",
      "      <td>2.990/4</td>\n",
      "      <td>3.095/7</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>深市</td>\n",
      "      <td>2.850/1</td>\n",
      "      <td>2.700/2</td>\n",
      "      <td>2.650/3</td>\n",
      "      <td>2.620/4</td>\n",
      "      <td>2.801/7</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">货基</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>511990</td>\n",
      "      <td>511900</td>\n",
      "      <td>511660</td>\n",
      "      <td>511690</td>\n",
      "      <td>511830</td>\n",
      "      <td>511850</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>99.9969</td>\n",
      "      <td>99.9972</td>\n",
      "      <td>100.0007</td>\n",
      "      <td>100.0007</td>\n",
      "      <td>100.0014</td>\n",
      "      <td>100.0016</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">新股</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>A股</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>港股</td>\n",
      "      <td>盛世大联</td>\n",
      "      <td>泓盈控股</td>\n",
      "      <td>君百延</td>\n",
      "      <td>江苏创新</td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">可转债</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>申购</td>\n",
      "      <td>亨通转债</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>上市</td>\n",
      "      <td>中信转债</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "  </tbody>\n",
      "</table>\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "# 导入基本库\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import json\n",
    "from six import StringIO\n",
    "from datetime import timedelta,date\n",
    "\n",
    "# 导入自定义库\n",
    "import sys\n",
    "sys.path.append('/home/jquser')\n",
    "from ds_sina import *\n",
    "from ds_wall import *\n",
    "from ds_jisilu import *\n",
    "#研究、策略中区别配置\n",
    "try:\n",
    "    #策略中必须导入kuanke.user_space_api包，用于支持read_file\n",
    "    from kuanke.user_space_api import read_file,get_price,normalize_code\n",
    "except:\n",
    "    pass\n",
    "    \n",
    "\n",
    "FILE_PATH='Data/'\n",
    "\n",
    "\n",
    "    \n",
    "#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n",
    "#日报：市场关注（微信）\n",
    "#-------------------------------------------------------------------------------------------------------------------------------------------------\n",
    "        \n",
    "# 基金溢价\n",
    "def get_discount_fund2(value=3.0,type_list={'index_etf':'ETF','index_lof':'LOF','t0_qdii':'QDII'}):\n",
    "    #标题\n",
    "    info='\\n基金\\n'\n",
    "    info1='溢价'\n",
    "    info2='折价'\n",
    "    for code,name in type_list.items():\n",
    "        #爬取集思录数据\n",
    "        df=get_jsl_data(code)\n",
    "        #去除溢价率中的百分号\n",
    "        df['discount_rt']=df['discount_rt'].str.replace('%','')\n",
    "        #转换为浮点数字\n",
    "        df['discount_rt']=df['discount_rt'].astype('float')\n",
    "        #筛选所有符合标准的溢价和折价\n",
    "        info1=info1+' %s%s'%(name,str(len(df[df['discount_rt']>=value])).rjust(2))\n",
    "        info2=info2+' %s%s'%(name,str(len(df[df['discount_rt']<=-value])).rjust(2))\n",
    "    return info+info1+'\\n'+info2+'\\n'\n",
    "\n",
    "\n",
    "# 套利\n",
    "def get_discount_fund():\n",
    "    #爬取集思录数据\n",
    "    df=get_jsl_data('t0_qdii')\n",
    "    df=df[df['fund_id']=='162411']\n",
    "    print df\n",
    "    info=pd.DataFrame(\n",
    "            data=[\n",
    "                ['#套利','','','','',''],\n",
    "                ['华宝油气','溢价率','当前价格',\n",
    "                 '%s日净值'%(df['nav_dt'].iloc[0][-2:]),'%s日估值'%(df['est_val_dt'].iloc[0][-2:]),\n",
    "                 '指数涨幅'],\n",
    "                ['',df['discount_rt'].iloc[0],df['price'].iloc[0],\n",
    "                 round(float(df['fund_nav'].iloc[0]),3),round(float(df['estimate_value'].iloc[0]),3),\n",
    "                df['ref_increase_rt'].iloc[0]]],\n",
    "            columns=range(6)) \n",
    "    return info\n",
    "\n",
    "\n",
    "\n",
    "# 新股\n",
    "def get_new_stock():\n",
    "    #爬取集思录数据\n",
    "    a_df=get_jsl_data('new_stock')\n",
    "    #获取今日日期并转换格式\n",
    "    today=pd.datetime.today().strftime('%m-%d')\n",
    "    #筛选A股今日新股（生成股票名称列表）\n",
    "    #爬取集思录数据\n",
    "    h_df=get_jsl_data('hnew_stock')\n",
    "    #筛选港股今日新股（生成股票名称列表）\n",
    "    info=pd.DataFrame(\n",
    "                  data=[['#新股','','','','',''],\n",
    "                        ['A股']+a_df[a_df['apply_dt'].str[0:5]==today]['stock_nm'][0:4].tolist(),\n",
    "                        ['港股']+h_df[h_df['apply_dt'].str[0:5]==today]['stock_nm'][0:4].tolist()],\n",
    "                  columns=range(6)) \n",
    "    #返回信息\n",
    "    return info\n",
    "\n",
    "\n",
    "#可转债\n",
    "def get_new_cbond():\n",
    "    #爬取集思录数据\n",
    "    df=get_jsl_data('new_cbond')\n",
    "    #获取今日日期并转换格式\n",
    "    today=pd.datetime.today().strftime('%m-%d')\n",
    "    #筛选今日可申购转债（ap_flag为A）\n",
    "    #筛选今日上市转债（ap_flag为D）\n",
    "    info=pd.DataFrame(\n",
    "        data=[['#可转债','','','','',''],\n",
    "            ['申购']+df[df['ap_flag'].str[0:1]=='A']['bond_nm'].tolist(),\n",
    "            ['上市']+df[df['ap_flag'].str[0:1]=='D']['bond_nm'].tolist()],\n",
    "        columns=range(6)) \n",
    "    #返回信息\n",
    "    return info\n",
    "\n",
    "\n",
    "#逆回购\n",
    "def get_rep_money():\n",
    "    #爬取集思录数据（上海逆回购）\n",
    "    hs_df=get_jsl_data('sh_money')[0:5] \n",
    "    \n",
    "    #爬取集思录数据（深圳逆回购）\n",
    "    ss_df=get_jsl_data('sz_money')[0:5] \n",
    "\n",
    "    info=pd.DataFrame(\n",
    "        data=[\n",
    "            ['#逆回购','','','','',''],\n",
    "            ['沪市']+['%s/%s'%(price,d) for d,price in zip([1,2,3,4,7],hs_df['price'].tolist())],\n",
    "            ['深市']+['%s/%s'%(price,d) for d,price in zip([1,2,3,4,7],ss_df['price'].tolist())]\n",
    "            ],\n",
    "        columns=range(6))  \n",
    "    #返回信息\n",
    "    return info\n",
    "\n",
    "\n",
    "#货基\n",
    "def get_t0_money():\n",
    "    #待选货基列表\n",
    "    fund_list=['511850','511800','511830','511700','511820','511900',\n",
    "        '511690','511810','511660','511990','511880']\n",
    "    #爬取集思录数据\n",
    "    df=get_jsl_data('t0_money')[['fund_id','sell_price']] \n",
    "    #按照待选列表筛选数据\n",
    "    df=df[df['fund_id'].isin(fund_list)]\n",
    "    #转换价格为浮点类型，否则排序不准确\n",
    "    df['sell_price']=np.round(df['sell_price'].astype('float'),4)\n",
    "    #从高到底排序,取价格最低的前4个\n",
    "    df=df.sort('sell_price',ascending=True)[0:6]\n",
    "\n",
    "    info=pd.DataFrame(\n",
    "        data=[\n",
    "            ['#货基','','','','',''],\n",
    "            df['fund_id'].tolist(),df['sell_price'].tolist()],\n",
    "        columns=range(6))  \n",
    "\n",
    "    #返回信息\n",
    "    return info\n",
    "\n",
    "\n",
    "#指数估值      \n",
    "def get_value_index():\n",
    "    #指数关注列表       \n",
    "    index_list=[\n",
    "        'HSCEI',#恒生国企,\n",
    "        'HSI',#恒生指数\n",
    "        'SPX',#恒生指数\n",
    "        '000902.SH',#全市场\n",
    "        '000300.SH',#沪深300\n",
    "        '000905.SH',#中证500\n",
    "        '399006.sz',#创业板指\n",
    "        '399005.sz',#中小板指  \n",
    "        '000991.SH',#全指医药\n",
    "        '000992.SH',#全指金融\n",
    "        '000990.SH',#全指消费\n",
    "        '000993.SH',#全指信息\n",
    "        '399812.sz',#中证养老\n",
    "        '000807.SH',#食品饮料\n",
    "        '000922.SH',#中证红利 \n",
    "        '399967.sz',#中证军工\n",
    "        '399989.sz',#中证医疗\n",
    "        '000827.SH',#中证环保\n",
    "        '399971.sz',#中证传媒\n",
    "        '399986.sz',#中证银行\n",
    "        '399975.sz',#全指证券\n",
    "        '000015.SH',#上证红利\n",
    "        ]\n",
    "    #读取指数分析表\n",
    "    df=pd.read_csv(StringIO(read_file(FILE_PATH+'idx_value.csv')),index_col=0)\n",
    "    df=df[df['aid']==10]\n",
    "    #按照关注列表筛选\n",
    "    df=df[df.index.isin(index_list)]\n",
    "    #全市场估值\n",
    "    code='000902.SH'\n",
    "    #当前估值、百分位、区间\n",
    "    #区间为极低的前5个指数名称\n",
    "    info=pd.DataFrame(\n",
    "        data=[\n",
    "            ['#估值','','','','',''],\n",
    "            ['全市场','PE:%s'%(df.ix[code,'pe_e']),'%s%%%s'%(df.ix[code,'pe_e_ratio'],df.ix[code,'pe_e_state']),\n",
    "                      'PB:%s'%(df.ix[code,'pb_e']),'%s%%%s'%(df.ix[code,'pb_e_ratio'],df.ix[code,'pb_e_state'])],\n",
    "            ['关注']+df[df['pe_e_state']=='极低']['name'][0:5].tolist() ],\n",
    "        columns=range(6))  \n",
    "    #返回信息\n",
    "    return info\n",
    "    \n",
    "\n",
    "    \n",
    "#实时价格    \n",
    "def get_real_price():\n",
    "    #获取wall实时价格\n",
    "    price=Wall.real_price('000001.SS,399300.SZ,399006.SZ,399001.SZ,\\\n",
    "SPX500INDEX,US30INDEX,NASINDEX,HKG33INDEX,GER30INDEX,SP500VIXINDEX,UK100INDEX,BDIINDEX,\\\n",
    "USDOLLARINDEX,USDCNH,USDCNY,USDJPY,HKDCNY,BTCUSD,\\\n",
    "XAUUSD,XAGUSD,\\\n",
    "UKOIL,USOIL,\\\n",
    "US10YEAR,US5YEAR,US3YEAR,CHINA10YEAR,CHINA5YEAR,CHINA3YEAR')\n",
    "    info=pd.DataFrame(\n",
    "        data=[\n",
    "            ['#股指','','','','',''],\n",
    "            ['上证综指','沪深300','创业板指','深证成指','恒生指数','BDI指数'],\n",
    "            [price['000001.SS'],price['399300.SZ'],price['399006.SZ'],price['399001.SZ'],price['HKG33INDEX'],price['BDIINDEX']],\n",
    "            ['标普500','道琼斯','纳斯达克','德国DAX','英国富时','VIX波动'],\n",
    "            [price['SPX500INDEX'],price['US30INDEX'],price['NASINDEX'],price['GER30INDEX'],\n",
    "             price['UK100INDEX'],price['SP500VIXINDEX']],\n",
    "            ['#商品','','','','',''],\n",
    "            ['黄金','白银','金银比','布原油','WTI原油','金油比'],\n",
    "            [price['XAUUSD'],price['XAGUSD'],round(price['XAUUSD']/price['XAGUSD'],3),\n",
    "            price['UKOIL'],price['USOIL'],round(price['XAUUSD']/price['UKOIL'],3)],\n",
    "            ['#外汇','','','','',''],\n",
    "            ['美元指数','离岸￥','在岸￥','港币/￥','＄/日元 ','比特币/＄'],\n",
    "            [price['USDOLLARINDEX'],price['USDCNH'],price['USDCNY'],price['HKDCNY'],price['USDJPY'],price['BTCUSD']],\n",
    "            ['#国债','','','','',''],\n",
    "            ['美国10年','美国5年','美国3年','中国10年','中国5年','中国3年'],\n",
    "            [price['USDOLLARINDEX'],price['USDJPY'],price['USDCNH'],\n",
    "            price['CHINA10YEAR'],price['CHINA5YEAR'],price['CHINA3YEAR']]\n",
    "            ],\n",
    "          columns=range(6)) \n",
    "\n",
    "    #返回信息\n",
    "    return info       \n",
    "    \n",
    "    \n",
    "#获取关注信息     \n",
    "def get_market_watch():\n",
    "\n",
    "    df=pd.concat([get_value_index(),get_real_price(),get_discount_fund(),get_rep_money(),\n",
    "                  get_t0_money(),get_new_stock(),get_new_cbond()])\n",
    "    return df.to_html(\n",
    "            index=False,\n",
    "            header=False,\n",
    "            classes='')\\\n",
    "            .replace('<table border=\"1\" class=\"dataframe\">','<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\" rules=\"rows\" frame=\"void\">')\\\n",
    "            .replace('<td>#','<td style=\"text-align:left;font-weight:bold\">')\\\n",
    "            .replace('<tbody>','<tbody style=\"text-align:right;font-size:8px\">')\\\n",
    "            .replace('None','')\n",
    "\n",
    "pd.set_option('display.max_rows', 10000)\n",
    "pd.set_option('display.max_columns', 10000)\n",
    "pd.set_option('display.width', 10000)\n",
    "\n",
    "#get_discount_fund()\n",
    "print get_market_watch()    \n",
    "# print get_market_watch()\n",
    "# print get_t0_money()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table border=\"1\" class=\"dataframe border=\"1\" cellspacing=\"0\" cellpadding=\"6\" rules=\"rows\" frame=\"void\"\">\n",
    "  <thead>\n",
    "    <tr style=\"text-align: right;\">\n",
    "      <th>0</th>\n",
    "      <th>1</th>\n",
    "      <th>2</th>\n",
    "      <th>3</th>\n",
    "      <th>4</th>\n",
    "      <th>5</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td align=\"left\">逆回购</td>\n",
    "      <td></td>\n",
    "      <td></td>\n",
    "      <td></td>\n",
    "      <td></td>\n",
    "      <td></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>204001</td>\n",
    "      <td>204002</td>\n",
    "      <td>204003</td>\n",
    "      <td>204004</td>\n",
    "      <td>204007</td>\n",
    "      <td>204014</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>2.340</td>\n",
    "      <td>2.300</td>\n",
    "      <td>2.390</td>\n",
    "      <td>2.310</td>\n",
    "      <td>2.435</td>\n",
    "      <td>2.505</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>131810</td>\n",
    "      <td>131811</td>\n",
    "      <td>131800</td>\n",
    "      <td>131809</td>\n",
    "      <td>131801</td>\n",
    "      <td>131802</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>1.701</td>\n",
    "      <td>1.600</td>\n",
    "      <td>1.800</td>\n",
    "      <td>1.900</td>\n",
    "      <td>2.250</td>\n",
    "      <td>2.302</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td align=\"left\">货基</td>\n",
    "      <td></td>\n",
    "      <td></td>\n",
    "      <td></td>\n",
    "      <td></td>\n",
    "      <td></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>511820</td>\n",
    "      <td>511810</td>\n",
    "      <td>511900</td>\n",
    "      <td>511800</td>\n",
    "      <td>None</td>\n",
    "      <td>None</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td>100.0024</td>\n",
    "      <td>100.0034</td>\n",
    "      <td>100.0045</td>\n",
    "      <td>100.0057</td>\n",
    "      <td>None</td>\n",
    "      <td>None</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('/home/jquser')\n",
    "from at_watch import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<table border=\"1\" class=\"dataframe border=\"1\" cellspacing=\"0\" cellpadding=\"6\" rules=\"rows\" frame=\"void\"\">\n",
      "  <tbody style=\"text-align:right;\">\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">指数估值</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>全市场</td>\n",
      "      <td>PE:24.66</td>\n",
      "      <td>3.62%极低</td>\n",
      "      <td>PB:1.85</td>\n",
      "      <td>2.63%极低</td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>关注</td>\n",
      "      <td>全指消费</td>\n",
      "      <td>沪深300</td>\n",
      "      <td>中证500</td>\n",
      "      <td>全指信息</td>\n",
      "      <td>中证红利</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">股指</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>上证综指</td>\n",
      "      <td>沪深300</td>\n",
      "      <td>创业板指</td>\n",
      "      <td>深证成指</td>\n",
      "      <td>恒生指数</td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>2692.369</td>\n",
      "      <td>3353.381</td>\n",
      "      <td>1363.527</td>\n",
      "      <td>8160.279</td>\n",
      "      <td>27912.97</td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>标普500</td>\n",
      "      <td>道琼斯</td>\n",
      "      <td>纳斯达克</td>\n",
      "      <td>德国DAX</td>\n",
      "      <td>英国富时</td>\n",
      "      <td>VIX波动</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>2745.73</td>\n",
      "      <td>25439.39</td>\n",
      "      <td>7426.95</td>\n",
      "      <td>11089.79</td>\n",
      "      <td>7197.01</td>\n",
      "      <td>16.22</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">商品</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>黄金</td>\n",
      "      <td>白银</td>\n",
      "      <td>金银比</td>\n",
      "      <td>布原油</td>\n",
      "      <td>WTI原油</td>\n",
      "      <td>金油比</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>1312.94</td>\n",
      "      <td>15.582</td>\n",
      "      <td>84.26</td>\n",
      "      <td>64.94</td>\n",
      "      <td>54.65</td>\n",
      "      <td>20.218</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">外汇</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>美元指数</td>\n",
      "      <td>美元/日元</td>\n",
      "      <td>离岸人民币</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>97.0615</td>\n",
      "      <td>110.31</td>\n",
      "      <td>6.7866</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">国债</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>美国10年</td>\n",
      "      <td>美国5年</td>\n",
      "      <td>美国3年</td>\n",
      "      <td>中国10年</td>\n",
      "      <td>中国5年</td>\n",
      "      <td>中国3年</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>97.0615</td>\n",
      "      <td>110.31</td>\n",
      "      <td>6.7866</td>\n",
      "      <td>3.083</td>\n",
      "      <td>2.881</td>\n",
      "      <td>2.654</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">套利</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>华宝油气</td>\n",
      "      <td>溢价率</td>\n",
      "      <td>当前价格</td>\n",
      "      <td>13日净值</td>\n",
      "      <td>14日估值</td>\n",
      "      <td>指数涨幅</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td></td>\n",
      "      <td>-1.08%</td>\n",
      "      <td>0.506</td>\n",
      "      <td>0.511</td>\n",
      "      <td>0.505</td>\n",
      "      <td>1.28%</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">逆回购</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>204001</td>\n",
      "      <td>204002</td>\n",
      "      <td>204003</td>\n",
      "      <td>204004</td>\n",
      "      <td>204007</td>\n",
      "      <td>204014</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>2.435</td>\n",
      "      <td>2.360</td>\n",
      "      <td>2.325</td>\n",
      "      <td>2.395</td>\n",
      "      <td>2.475</td>\n",
      "      <td>2.650</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>131810</td>\n",
      "      <td>131811</td>\n",
      "      <td>131800</td>\n",
      "      <td>131809</td>\n",
      "      <td>131801</td>\n",
      "      <td>131802</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>2.313</td>\n",
      "      <td>2.300</td>\n",
      "      <td>2.181</td>\n",
      "      <td>2.281</td>\n",
      "      <td>2.428</td>\n",
      "      <td>2.560</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">货基</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>511800</td>\n",
      "      <td>511900</td>\n",
      "      <td>511810</td>\n",
      "      <td>511820</td>\n",
      "      <td>511700</td>\n",
      "      <td>511830</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>100.0235</td>\n",
      "      <td>100.0255</td>\n",
      "      <td>100.0263</td>\n",
      "      <td>100.0275</td>\n",
      "      <td>100.0319</td>\n",
      "      <td>100.0327</td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">新股</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>A股</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>港股</td>\n",
      "      <td>嘉艺控股</td>\n",
      "      <td>域高国际</td>\n",
      "      <td>名仕快相</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td style=\"text-align:left;font-weight:bold\">可转债</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>申购</td>\n",
      "      <td>072891</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "    <tr>\n",
      "      <td>上市</td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "      <td></td>\n",
      "    </tr>\n",
      "  </tbody>\n",
      "</table>\n"
     ]
    }
   ],
   "source": [
    "print get_market_watch()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.简讯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "No module named jsl",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m\u001b[0m",
      "\u001b[0;31mImportError\u001b[0mTraceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-75d74308500f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     17\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"/home/jquser/\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[0;31m#导入自己的库\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mjsl\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     20\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0msina\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     21\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mmail\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mImportError\u001b[0m: No module named jsl"
     ]
    }
   ],
   "source": [
    "#导入函数库\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import json\n",
    "from six import StringIO\n",
    "from datetime import timedelta,date\n",
    "\n",
    "#聚源数据库\n",
    "from jqdata import jy\n",
    "\n",
    "import os\n",
    "\n",
    "#引入自定义（jsl）库\n",
    "#保存当前目录\n",
    "path=os.getcwd()\n",
    "#更改到用户根目录为\n",
    "os.chdir(\"/home/jquser/\")\n",
    "#导入自己的库\n",
    "from jsl import *\n",
    "from sina import *\n",
    "from mail import *\n",
    "from index_base import *\n",
    "#恢复当前目录\n",
    "os.chdir(path)\n",
    "#导入自定义库（从集思录爬取数据）\n",
    "\n",
    "\n",
    "#读取指数估值分析表        \n",
    "def read_analysis_index(file_root='../指数/'):\n",
    "    #文件名\n",
    "    file_name='%s%s.csv'%(file_root,'index_analysis') \n",
    "    #从cvs文件恢复数据\n",
    "    df=pd.read_csv(file_name,index_col=0,parse_dates=False)\n",
    "    return df\n",
    "\n",
    "#基金溢价\n",
    "def get_discount_fund(value=3.0,type_list={'index_etf':'ETF','index_lof':'LOF','t0_qdii':'QDII'}):\n",
    "    #标题\n",
    "    info='基金'\n",
    "    info1='溢价'\n",
    "    info2='折价'\n",
    "    for code,name in type_list.items():\n",
    "        #爬取集思录数据\n",
    "        df=get_jsl_data(code)\n",
    "        #去除溢价率中的百分号\n",
    "        df['discount_rt']=df['discount_rt'].str.replace('%','')\n",
    "        #转换为浮点数字\n",
    "        df['discount_rt']=df['discount_rt'].astype('float')\n",
    "        #筛选所有符合标准的溢价和折价\n",
    "        info1=info1+' %s%s'%(name,str(len(df[df['discount_rt']>=value])).rjust(2))\n",
    "        info2=info2+' %s%s'%(name,str(len(df[df['discount_rt']<=-value])).rjust(2))\n",
    "    return info+'\\n'+info1+'\\n'+info2+'\\n'\n",
    "\n",
    "#新股\n",
    "def get_new_stock():\n",
    "    #标题\n",
    "    info='新股\\n'  \n",
    "    #爬取集思录数据\n",
    "    df=get_jsl_data('new_stock')\n",
    "    #获取今日日期并转换格式\n",
    "    today=pd.datetime.today().strftime('%m-%d')\n",
    "    #筛选A股今日新股（生成股票名称列表）\n",
    "    info=info+'A股 %s\\n'%(trans_list(df[df['apply_dt'].str[0:5]==today]['stock_nm'].tolist()))\n",
    "    #爬取集思录数据\n",
    "    df=get_jsl_data('hnew_stock')\n",
    "    #筛选港股今日新股（生成股票名称列表）\n",
    "    info=info+'港股 %s\\n'%(trans_list(df[df['apply_dt'].str[0:5]==today]['stock_nm'].tolist()))\n",
    "    #返回信息\n",
    "    return info\n",
    "\n",
    "#可转债\n",
    "def get_new_cbond():\n",
    "    #标题\n",
    "    info='可转债\\n'  \n",
    "    #爬取集思录数据\n",
    "    df=get_jsl_data('new_cbond')\n",
    "    #获取今日日期并转换格式\n",
    "    today=pd.datetime.today().strftime('%m-%d')\n",
    "    #筛选今日可申购转债\n",
    "    info=info+'申购 %s\\n'%(trans_list(df[df['ap_flag'].str[0:1]=='A']['apply_cd'].tolist()))\n",
    "    #筛选今日上市转债\n",
    "    info=info+'上市 %s\\n'%(trans_list(df[df['ap_flag'].str[0:1]=='D']['bond_nm'].tolist()).replace('转债',''))\n",
    "    #返回信息\n",
    "    return info\n",
    "\n",
    "#逆回购\n",
    "def get_rep_money():\n",
    "    #标题\n",
    "    info='逆回购\\n' \n",
    "    #天数标题\n",
    "    #info=info+'天 1     2     3     4     5     6     7     \\n'\n",
    "    #爬取集思录数据\n",
    "    df=get_jsl_data('sh_money') \n",
    "    df['price']=df['price'].str[:-2].str.rjust(4)\n",
    "    #沪市逆回购价格聊表（取7天内）\n",
    "    info=info+'沪 %s\\n'%(trans_list(df['price'].tolist()[0:7]))\n",
    "    #爬取集思录数据\n",
    "    df=get_jsl_data('sz_money') \n",
    "    df['price']=df['price'].str[:-2].str.rjust(4)\n",
    "    #深市逆回购价格列表（取7天内）\n",
    "    info=info+'深 %s\\n'%(trans_list(df['price'].tolist()[0:7]))\n",
    "    #返回信息\n",
    "    return info\n",
    "\n",
    "#货基\n",
    "def get_t0_money():\n",
    "    #标题\n",
    "    info='T+0货基\\n' \n",
    "    #待选货基列表\n",
    "    fund_list=['511800','511830','511700','511820','511900',\n",
    "        '511690','511810','511660','511850','511990','511880']\n",
    "    #爬取集思录数据\n",
    "    df=get_jsl_data('t0_money')[['fund_id','sell_price']] \n",
    "    #按照待选列表筛选数据\n",
    "    df=df[df['fund_id'].isin(fund_list)]\n",
    "    #转换价格格式及排序\n",
    "    df['sell_price']=df['sell_price'].astype('float32')\n",
    "    df=df.sort('sell_price',ascending=True)\n",
    "    #取价格最低的前4个\n",
    "    df=df[0:4]\n",
    "    #去除多余的小数位，按照7位补足前面空格\n",
    "    df['sell_price']=df['sell_price'].astype('str').str[:-2].str.rjust(6)\n",
    "    #代码\n",
    "    info=info+'代码 %s\\n'%(trans_list(df['fund_id'].tolist()))\n",
    "    #价格\n",
    "    info=info+'价格 %s\\n'%(trans_list(df['sell_price'].tolist()))\n",
    "    #返回信息\n",
    "    return info\n",
    "\n",
    "#指数估值      \n",
    "def get_value_index():\n",
    "    #标题\n",
    "    info='指数估值\\n'\n",
    "    df=read_index_analysis()\n",
    "    df=df[df.index.isin(index_use_list)]\n",
    "    #全市场估值\n",
    "    code='000902.XSHG'\n",
    "    info=info+'全市场 %s %s%% %s\\n'%(df.ix[code,'pe'],df.ix[code,'pe_ratio'],df.ix[code,'pe_state'])\n",
    "    #低估的\n",
    "    info=info+'关注　 %s\\n'%(trans_list(df[df['pe_state']=='极低']['index'][0:3].tolist()))\n",
    "    #返回信息\n",
    "    return info\n",
    "    \n",
    "#商品价格\n",
    "def get_price_commodity():\n",
    "    #标题\n",
    "    info='商品\\n' \n",
    "    #爬取新浪实时数据\n",
    "    data=get_sina_hf_price(['hf_GC','hf_SI','hf_OIL'])  \n",
    "    #沪市逆回购价格聊表（取7天内）\n",
    "    info=info+'金%s 银%s 油%s 金银比%.2f\\n'%(data['hf_GC'],\n",
    "        data['hf_SI'],data['hf_OIL'],float(data['hf_GC'])/float(data['hf_SI']))\n",
    "    #返回信息\n",
    "    return info   \n",
    "    \n",
    "#去除列表中的多余字符\n",
    "def trans_list(lst):\n",
    "    #解决中文乱码问题\n",
    "    data=json.dumps(lst,encoding='UTF-8',ensure_ascii=False)\n",
    "    #去除列表中的多余字符\n",
    "    data=data.replace('\"','').replace(',','').replace('[','').replace(']','')\n",
    "    #返回字符串，如果是无内容则为'-'\n",
    "    return (data if data!='' else '-')\n",
    "    \n",
    "#获取关注信息     \n",
    "def get_market_notice():\n",
    "    (info_1,info_2,info_3,info_4,info_5,info_6,info_7)='','','','','','',''\n",
    "    try:\n",
    "        info_1=get_value_index()\n",
    "        info_2=get_new_cbond()\n",
    "        info_3=get_new_stock()\n",
    "        info_4=get_price_commodity()\n",
    "        info_5=get_rep_money()\n",
    "        info_6=get_discount_fund()\n",
    "        info_7=get_t0_money()\n",
    "    except:\n",
    "        raise \n",
    "        #pass\n",
    "    notice='日报：市场关注\\n\\n%s\\n%s\\n%s\\n%s\\n%s\\n%s\\n%s'%(\n",
    "        info_1,\n",
    "        info_2,\n",
    "        info_3,\n",
    "        info_4,\n",
    "        info_5,\n",
    "        info_6,\n",
    "        info_7,\n",
    "        )\n",
    "    return notice#.replace('\\n','<br>')\n",
    "\n",
    "notice=get_market_notice()\n",
    "print notice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "import sys\n",
    "sys.path.append('/home/jquser')\n",
    "from wall import *\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "股指\n",
      "上证综指　 沪深300　  创业板指\n",
      "2784.03    3426.19    1548.44   \n",
      "标普500　  道琼斯　　 纳斯达克\n",
      "2716.31    24216.05   7503.68   \n",
      "恒生指数　 德国DAX　  VIX波动\n",
      "28567.38   12177.23   16.85     \n",
      "\n",
      "商品\n",
      "黄金　　　 白银　　　 金银比\n",
      "1247.70    15.99      78.03     \n",
      "布原油　　 WTI原油　  金油比\n",
      "77.23      73.03      16.16     \n",
      "\n",
      "外汇\n",
      "美元指数　 美元/日元  离岸人民币\n",
      "95.30      110.51     6.65      \n",
      "\n",
      "国债\n",
      "美国10年　 美国5年　  美国3年\n",
      "2.84       2.72       2.60      \n",
      "中国10年　 中国5年　  中国3年\n",
      "3.57       3.44       3.39      \n",
      "\n"
     ]
    }
   ],
   "source": [
    "def real_price(code):  \n",
    "    \"\"\"\n",
    "    获取wall网站财经数据实时价格\n",
    "    \n",
    "    code：代码，str，多个代码用','分隔\n",
    "    返回：代码和最新价格的字典，dict\n",
    "    \n",
    "    返回数据实例\n",
    "    数据在data、snapshot中\n",
    "    fields为字段名\n",
    "    \n",
    "    A股指数返回数据：\n",
    "    无en_name字段\n",
    "    {u'code': 200, u'data': {u'snapshot': {u'fields': [u'prod_name', u'last_px', u'px_change', u'px_change_rate', \n",
    "    u'high_px', u'low_px', u'open_px', u'preclose_px', u'business_amount', u'trade_status', u'securities_type', \n",
    "    u'update_time', u'price_precision', u'en_name'], u'000001.SS': [u'\\u4e0a\\u8bc1\\u6307\\u6570', 2786.8966, \n",
    "    -26.280899999999747, -0.9342069599234228, 2825.9872, 2782.4464, 2799.9039, 2813.1775, 11835790700, u'BREAK', \n",
    "    u'index', 1530169200, 2, u'000001.SS']}}}\n",
    "    \n",
    "    海外指数、商品、外汇等返回数据：\n",
    "    有en_name字段\n",
    "    {u'code': 200, u'data': {u'snapshot': {u'fields': [u'en_name', u'prod_name', u'last_px', u'px_change', \n",
    "    u'px_change_rate', u'high_px', u'low_px', u'open_px', u'preclose_px', u'business_amount', u'trade_status',\n",
    "    u'securities_type', u'update_time', u'price_precision', u'week_52_high', u'week_52_low'], u'SPX500INDEX': \n",
    "    [u'SPX', u'\\u6807\\u666e500', 2699.63, -23.43, -0.86, 2746.09, 2699.38, 2723.06, 2723.06, 0, u'ENDTR', u'indice',\n",
    "    1530129598, 2, 2872.87, 2406.55]}}}\n",
    "    \"\"\"\n",
    "    \n",
    "    # url，换行前面不能有空格和tab\n",
    "    url='https://forexdata.wallstreetcn.com/real?en_prod_code={en_prod_code}&\\\n",
    "fields=en_name,prod_name,last_px,px_change,px_change_rate,high_px,low_px,open_px,preclose_px,\\\n",
    "business_amount,business_balance,market_value,turnover_ratio,dyn_pb_rate,amplitude,pe_rate,bps,\\\n",
    "hq_type_code,trade_status,bid_grp,offer_grp,business_amount_in,business_amount_out,circulation_value,\\\n",
    "securities_type,update_time,price_precision,week_52_high,week_52_low'.format(en_prod_code=code)\n",
    "    # 请求数据\n",
    "    response=requests.get(url)\n",
    "    # json解析,有效数据在snapshot中\n",
    "    json_data=json.loads(response.text).get(\"data\").get(\"snapshot\")\n",
    "    # 删除fields\n",
    "    json_data.pop('fields')\n",
    "    # 判断code中\n",
    "    dot_count=code.strip(',').count('.')\n",
    "    if dot_count==0 : \n",
    "        n=2\n",
    "    elif dot_count>0 : \n",
    "        n=1\n",
    "    data={}\n",
    "    for item in json_data:\n",
    "        data[item]=float(json_data.get(item)[n])\n",
    "    return data\n",
    "\n",
    "\n",
    "# print real_price('SPX500INDEX,399300.SZ')\n",
    "\n",
    "\n",
    "\n",
    "def get_real_price():\n",
    "    #获取wall实时价格\n",
    "    price=Wall.real_price('000001.SS,399300.SZ,399006.SZ,\\\n",
    "SPX500INDEX,US30INDEX,NASINDEX,HKG33INDEX,GER30INDEX,SP500VIXINDEX,\\\n",
    "USDOLLARINDEX,USDCNH,USDJPY,\\\n",
    "XAUUSD,XAGUSD,\\\n",
    "UKOIL,USOIL,\\\n",
    "US10YEAR,US5YEAR,US3YEAR,CHINA10YEAR,CHINA5YEAR,CHINA3YEAR')\n",
    "    info='\\n股指\\n' \n",
    "    info+='%s %s %s\\n'%('上证综指　','沪深300　 ','创业板指')\n",
    "    info+='%-10.2f %-10.2f %-10.2f\\n'%(price['000001.SS'],price['399300.SZ'],price['399006.SZ'])\n",
    "    info+='%s %s %s\\n'%('标普500　 ','道琼斯　　','纳斯达克')\n",
    "    info+='%-10.2f %-10.2f %-10.2f\\n'%(price['SPX500INDEX'],price['US30INDEX'],price['NASINDEX'])\n",
    "    info+='%s %s %s\\n'%('恒生指数　','德国DAX　 ','VIX波动')\n",
    "    info+='%-10.2f %-10.2f %-10.2f\\n'%(price['HKG33INDEX'],price['GER30INDEX'],price['SP500VIXINDEX'])\n",
    "    info+='\\n商品\\n' \n",
    "    info+='%s %s %s\\n'%('黄金　　　','白银　　　','金银比')\n",
    "    info+='%-10.2f %-10.2f %-10.2f\\n'%(price['XAUUSD'],price['XAGUSD'],price['XAUUSD']/price['XAGUSD'])    \n",
    "    info+='%s %s %s\\n'%('布原油　　','WTI原油　 ','金油比')\n",
    "    info+='%-10.2f %-10.2f %-10.2f\\n'%(price['UKOIL'],price['USOIL'],price['XAUUSD']/price['UKOIL'])  \n",
    "    info+='\\n外汇\\n' \n",
    "    info+='%s %s %s\\n'%('美元指数　','美元/日元 ','离岸人民币')\n",
    "    info+='%-10.2f %-10.2f %-10.2f\\n'%(price['USDOLLARINDEX'],price['USDJPY'],price['USDCNH'])  \n",
    "    info+='\\n国债\\n' \n",
    "    info+='%s %s %s\\n'%('美国10年　','美国5年　 ','美国3年')\n",
    "    info+='%-10.2f %-10.2f %-10.2f\\n'%(price['US10YEAR'],price['US5YEAR'],price['US3YEAR'])\n",
    "    info+='%s %s %s\\n'%('中国10年　','中国5年　 ','中国3年')\n",
    "    info+='%-10.2f %-10.2f %-10.2f\\n'%(price['CHINA10YEAR'],price['CHINA5YEAR'],price['CHINA3YEAR'])\n",
    "    #返回信息\n",
    "    return info   \n",
    "\n",
    "temp=get_real_price()\n",
    "print temp\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    #爬取新浪实时数据\n",
    "#     data=get_sina_hf_price(['hf_GC','hf_SI','hf_OIL'])  \n",
    "#     #金价、银价、油价、金银比\n",
    "#     info=info+'金%.2f 银%.2f 油%.2f 金银比%.2f\\n'%(float(data['hf_GC']),\n",
    "#         float(data['hf_SI']),float(data['hf_OIL']),float(data['hf_GC'])/float(data['hf_SI']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'33333'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'333335'[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
